package com.medicine.mapper;

import com.github.pagehelper.Page;
import com.medicine.annotation.AutoFill;
import com.medicine.dto.GroupPageQueryDTO;
import com.medicine.entity.Group;
import com.medicine.enumeration.OperationType;
import com.medicine.vo.GroupVO;
import com.medicine.vo.MedicineItemVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface GroupMapper {

    /**
     * 根据分类id查询药组的数量
     * @param id
     * @return
     */
    @Select("select count(id) from group where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 新增药组
     * @param group
     */
    @AutoFill(OperationType.INSERT)
    void insert(Group group);

    /**
     * 分页查询
     * @param groupPageQueryDTO
     * @return
     */
    Page<GroupVO> pageQuery(GroupPageQueryDTO groupPageQueryDTO);

    /**
     * 根据id查询药组
     * @param id
     * @return
     */
    @Select("select * from group where id = #{id}")
    Group getById(Long id);

    /**
     * 根据id删除药组
     * @param groupId
     */
    @Delete("delete from group where id = #{id}")
    void deleteById(Long groupId);

    /**
     * 根据Id修改药组
     * @param group
     */
    @AutoFill(OperationType.UPDATE)
    void update(Group group);

    /**
     * 动态条件查询药组
     * @param group
     * @return
     */
    List<Group> list(Group group);

    /**
     * 根据药组id查询药品选项
     * @param groupId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from group_medicine sd left join medicine d on sd.medicine_id = d.id " +
            "where sd.group_id = #{groupId}")
    List<MedicineItemVO> getMedicineItemByGroupId(Long groupId);

    /**
     * 根据条件统计药组数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

}
